package com.njupt.DynamicProgramming;

/**
 * @Author: wujiaming
 * @CreateTime: 2025/2/19 22:15
 * @Description:213. 打家劫舍 II 环形问题
 * @Version: 1.0
 */


public class Rob2_213 {

    public int rob(int[] nums) {
        if(nums.length == 1){
            return nums[0];
        }
        int result1 = rangeRob(nums,0,nums.length-2);
        int result2 = rangeRob(nums,1,nums.length-1);
        return result1 > result2 ? result1 : result2;
    }

    private int rangeRob(int[] nums, int start, int end) {

        if(start == end){
            return nums[start];
        }
        int[] dp = new int[nums.length];
        dp[start] = nums[start];
        dp[start + 1] = Math.max(nums[start],nums[start + 1]);
        for (int i = start + 2; i <= end ; i++) {
            dp[i] = Math.max(dp[i-1] ,dp[i-2] + nums[i]);
        }
        return dp[end];
    }

    public static void main(String[] args) {
        int[] nums = {1,2,3};
        Rob2_213 test = new Rob2_213();
        System.out.println(test.rob(nums));
    }
}
